#include "widget.h"
#include "ui_widget.h"
#include "addwidge.h"
#include "updatewidge.h"
#include <image.h>
#include <QSqlDatabase>
#include <QSqlQuery>
Widget::Widget(QString userName,QWidget *parent):QWidget(parent),ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->stackedWidget->setCurrentIndex(0);
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    CheckAndUpdateLastLoginTime(userName);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_SpeakButton_pressed()
{
    ui->SpeakButton->setText("松开后开始识别");
    m_audio=new Audio;
    m_audio->startAudio("D:\\desktop\\demo.pcm");//两个\表示转义
}

void Widget::on_SpeakButton_released()//录音识别按钮
{
    //停止录音才开始识别
    m_audio->stopAudio();
    ui->SpeakButton->setText("开始识别");
    //识别逻辑代码
    m_speech=new Speech;
    ui->textEdit->setText(m_speech->SpeechReconition("D:\\desktop\\demo.pcm"));
}


void Widget::on_ClearButton_clicked()
{
    ui->textEdit->setText("");
}

void Widget::on_AudioButton_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
}
void Widget::on_ImageButton_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
}

void Widget::on_UserManaButton_clicked()
{
    ui->stackedWidget->setCurrentIndex(2);
    //打开用户管理页面的同时开始加载数据库内的数据
    QSqlQuery request;
    request.exec("select * from user");
    int i=0;
    ui->tableWidget->setRowCount(request.size());
    ui->tableWidget->setAlternatingRowColors(true);
    while(request.next())
    {
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(request.value(0).toString()));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(request.value(1).toString()));
        ui->tableWidget->setItem(i++,2,new QTableWidgetItem(request.value(2).toDateTime().toString("yyyy-MM-dd HH:mm:ss")));
    }   
}

void Widget::on_OpenFileButton_clicked()
{
    //1.弹出打开文件的对话框
    ImageFileName=QFileDialog::getOpenFileName(this,"please choice image",QCoreApplication::applicationFilePath());
    if(ImageFileName.isEmpty())//如果需要判断打开的文件是不是图片可以用QMimeType
    {
        QMessageBox::warning(this,"Error!","Unable to open file");
    }
    //2.打开图片
    QPixmap pix(ImageFileName);
    pix=pix.scaled(ui->ImageView->geometry().size());
    //3.显示到Label上
    ui->ImageView->setPixmap(pix);
}

void Widget::on_ImageReconButton_clicked()
{
    QByteArray base64=imageEncode::imageTobase64(ImageFileName);
    //把请求数据包装成一个Body
    QByteArray postData = "image="+base64;
    img=new image;
    QString accessToken=img->getAccessToken();
    ui->Result->setText(img->submitRequest3(accessToken,postData));
}
//查询按钮
void Widget::on_SelectButton_clicked()
{
    //先获取输入框里面的查询信息
    QString query=ui->Query->text();
    QSqlQuery request;
    request.prepare(QString("SELECT * FROM user where username like '%%1%' or password like '%%1%'").arg(query));
    request.exec();
    ui->tableWidget->setRowCount(request.size());
    int i=0;
    while(request.next())
    {
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(request.value(0).toString()));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(request.value(1).toString()));
        ui->tableWidget->setItem(i++,2,new QTableWidgetItem(request.value(2).toDateTime().toString("yyyy-MM-dd HH:mm:ss")));
    }
}
void Widget::CheckAndUpdateLastLoginTime(QString userName)
{
    //修改帐号的“上次登录时间”
    QDateTime now = QDateTime::currentDateTime();
    QString   current = now.toString("yyyy-MM-dd HH:mm:ss");
    QSqlQuery request;
    request.prepare("select last_login_time from user where username=?");
    request.addBindValue(userName);
    request.exec();
    request.next();
    ui->LastLogin->setText(request.value(0).toDateTime().toString("yyyy-MM-dd HH:mm:ss"));
    request.prepare("update user set last_login_time = ? where username= ?");
    request.addBindValue(current);
    request.addBindValue(userName);
    request.exec();
}

void Widget::on_AddButton_clicked()
{
    AddWidge *a=new AddWidge;
    a->show();
    connect(a,&AddWidge::sendMsg,this,&Widget::on_UserManaButton_clicked);
}

void Widget::on_DeleteButton_clicked()
{
    QString userName=ui->tableWidget->currentItem()->text();
    int reply=QMessageBox::question(this,"Notify","Do you wan to delete User:"+userName+"?",QMessageBox::Yes|QMessageBox::No);
    QSqlQuery request;
    request.prepare("select username from user where username=?");
    request.addBindValue(userName);
    request.exec();
    request.next();
    QString  selectUserName=request.value(0).toString();
    if(selectUserName==userName && reply==QMessageBox::Yes)
    {
        QSqlQuery request;
        request.prepare("delete from user where username=?");
        request.addBindValue(userName);
        request.exec();
        this->on_UserManaButton_clicked();
    }
    else if(selectUserName!=userName && reply==QMessageBox::Yes)
    {
        QMessageBox::warning(this,"Error","User："+userName+" isn't exit!");
    }
}

void Widget::on_UpdateButton_clicked()
{
    QString userName=ui->tableWidget->currentItem()->text();
    if(userName!="")
    {
        UpdateWidge *u=new UpdateWidge(userName);
        u->show();
        connect(u,&UpdateWidge::sendData,this,&Widget::on_UserManaButton_clicked);
    }
    else
    {
        QMessageBox::warning(this,"Error","please choice a User");
    }

}
